package cn.bincker.test.service.impl;

import cn.bincker.test.entity.ClassRoom;
import cn.bincker.test.entity.Student;
import cn.bincker.test.mapper.first.StudentMapper;
import cn.bincker.test.mapper.second.ClassRoomMapper;
import cn.bincker.test.service.IStudentService;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 正常情况下，只有默认管理器生效
 */
@Service
public class MixinStudentServiceImpl implements IStudentService {
    private final StudentMapper studentMapper;
    private final ClassRoomMapper classRoomMapper;

    public MixinStudentServiceImpl(StudentMapper studentMapper, ClassRoomMapper classRoomMapper) {
        this.studentMapper = studentMapper;
        this.classRoomMapper = classRoomMapper;
    }

    @Override
    @Transactional("mixinTransactionManager")
    public Student addStudentToClassRoom(String studentName, String classRoomName) {
        ClassRoom classRoom = classRoomMapper.findByName(classRoomName);
        if(classRoom == null){
            classRoom = new ClassRoom();
            classRoom.setName(classRoomName);
            classRoomMapper.insert(classRoom);
        }
        Student student = new Student();
        student.setName(studentName);
        student.setClassRoomId(classRoom.getId());
        studentMapper.insert(student);
        //noinspection ConstantConditions
        if(true) throw new RuntimeException();
        return student;
    }
}
